﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class UserInfoPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Button btnSubmit;
        protected Label lblMessage;
        protected Label lblUserNo;
        protected ListBox lbxGroups;
        protected ListBox lbxRoles;
        protected RequiredFieldValidator rfvUserName;
        protected TextBox txtUserName;
        protected string userNo;
        protected ValidationSummary vsUser;

        protected virtual void btnSubmit_Click(object sender, EventArgs e)
        {
        }

        protected virtual void FillForm(User u)
        {
            this.lblUserNo.set_Text(u.UserNo);
            this.txtUserName.set_Text(u.UserName);
            string[] userRoles = Securitor.UserManager.GetUserRoles(this.userNo);
            this.lbxRoles.set_DataTextField("Title");
            this.lbxRoles.set_DataValueField("RoleNo");
            this.lbxRoles.set_DataSource(Securitor.RoleManager.GetItem(userRoles));
            this.lbxRoles.DataBind();
            this.lbxGroups.set_DataTextField("IndentTitle");
            this.lbxGroups.set_DataValueField("GroupNo");
            this.lbxGroups.set_DataSource(Securitor.GroupManager.GetUserGroups(this.userNo));
            this.lbxGroups.DataBind();
        }

        protected override void InitData()
        {
            base.InitData();
            this.userNo = Securitor.UserManager.GetUserNoByUserName(base.get_User().get_Identity().get_Name());
            if (!base.get_IsPostBack())
            {
                User u = this.LoadUser();
                this.FillForm(u);
            }
        }

        private void InitializeComponent()
        {
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
        }

        protected virtual User LoadUser()
        {
            User item = Securitor.UserManager.GetItem(this.userNo);
            Checker.CheckArgumentNull("User", item, true);
            return item;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }
    }
}

